/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.azon.ip.utils;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class ImageReader {
    
    public static BufferedImage ReadImage(String imgPath) throws IOException {
        BufferedImage img = ImageIO.read(new File(imgPath));
        return img;
    }
    
    public static int[][] ReadImageAsRGB(BufferedImage image) throws IOException {
        int[][] matrix = new int[image.getHeight()][image.getWidth()];
        
        BufferedImage new_img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = new_img.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        
        Raster raster = new_img.getData();
        
        for(int x=0; x < image.getWidth(); x++) {
            for(int y=0; y < image.getHeight(); y++) {
                matrix[y][x] = (raster.getSample(x, y, 0) << 16) + (raster.getSample(x, y, 1) << 8) + (raster.getSample(x, y, 2));
            }
        }
        return matrix;
    }
    
    public static int[][] ReadImageAsRGB(BufferedImage image, int band) {
        int[][] matrix = new int[image.getWidth()][image.getHeight()];
        
        Raster raster = image.getData();
        
        for(int x=0; x < image.getWidth(); x++) {
            for(int y=0; y < image.getHeight(); y++) {
                matrix[x][y] = raster.getSample(x, y, band);
            }
        }
        
        return matrix;
    }
    
    public static int[][] ReadImageAsRGB(String imgPath) {
        try {
            BufferedImage img = ReadImage(imgPath);
            return ReadImageAsRGB(img);
        } catch (IOException ex) {
            Logger.getLogger(ImageReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public static int[][] ReadImageAsGrayScale(BufferedImage image) {
        int matrix[][] = new int[image.getHeight()][image.getWidth()];
        int data[] = new int[image.getWidth()*image.getHeight()];
        
        BufferedImage new_img = new BufferedImage(matrix[0].length, matrix.length, BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = new_img.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        
        Raster raster = new_img.getData();
        
        for(int x=0; x < image.getWidth(); x++) {
            for(int y=0; y < image.getHeight(); y++) {
                matrix[y][x] = raster.getSample(x, y, 0) ;
            }
        }
         
        return matrix;
    }
    
    public static int ReadImagePixelGrayScale(BufferedImage image, int x, int y) {
        return image.getData().getSample(x, y, 0);
    }
    
     public static int[][] ReadImageAsGrayScale(String imgPath) {
        try {
            BufferedImage img = ReadImage(imgPath);
            return ReadImageAsGrayScale(img);
        } catch (IOException ex) {
            Logger.getLogger(ImageReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    // ReadHistogramInRGB
    public static int[][] ReadHistogramInRGB(BufferedImage image) {
        int[][] histogram = new int[3][255];
        
        for(int i=0; i < 3; i++) {
            for(int j=0; j < 255; j++) {
                histogram[i][j] = 0;
            }
        }
        
        for(int x =0; x < image.getWidth(); x++) {
            for(int y =0; y < image.getHeight(); y++) {
                histogram[0][image.getData().getSample(x, y, 0)]++;
                histogram[1][image.getData().getSample(x, y, 1)]++;
                histogram[2][image.getData().getSample(x, y, 2)]++;
            }
        }
        
        return histogram;
    }
    
    public static int[] ReadHistogramRGB(BufferedImage image, int band) {
        int[] histogram = new int[255];
        
            for(int j=0; j < 255; j++) {
                histogram[j] = 0;
            }
        
        for(int x =0; x < image.getWidth(); x++) {
            for(int y =0; y < image.getHeight(); y++) {
                histogram[image.getData().getSample(x, y, band)]++;
            }
        }
        
        return histogram;
    }
    
    // ReadHistogramInGreyScale
    public static int[] ReadHistogramInGrayScale(BufferedImage image) {
        
        int[] histogram = new int[255];
        int[] data = new int[image.getWidth()*image.getHeight()];
        
        for(int j=0; j < 255; j++) {
            histogram[j] = 0;
        }
        
        BufferedImage new_img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = new_img.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        new_img.getData().getSamples(0, 0, image.getWidth(), image.getHeight(), 0, data);
        
        for(int i=0; i < data.length; i++) {
            histogram[data[i]]++;
        }
        
        return histogram;
    }
    
    public static int[] ReadHistogramInGrayScale(int[][] matrix) {
        
        int[] histogram = new int[256];
        
        for(int j=0; j < 256; j++) {
            histogram[j] = 0;
        }
        
        for(int x = 0; x < matrix.length; x++) {
            for(int y=0; y < matrix[x].length; y++) {
                histogram[matrix[x][y]]++;
            }
        } 
        
        return histogram;
    }
    
}
